package com.apolo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.conexiones.WebService;

import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;

public class Contacto extends Activity {
	
	private EditText txtNombre;
	private EditText txtEmail;
	private EditText txtMensaje;
	private WebService webService;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contacto);
		
		this.txtNombre = (EditText) findViewById(R.id.txtNombre);
		this.txtEmail = (EditText) findViewById(R.id.txtEmail);
		this.txtMensaje = (EditText) findViewById(R.id.txtMensaje);
	}

	public void ubicacion(View view){
		Intent intent = new Intent(this, Ubicacion.class);
		startActivity(intent);
		this.finish();
	}
	
	public void informacion(View view){
		Intent intent = new Intent(this, Informacion.class);
		startActivity(intent);
		this.finish();
	}
	
	public void cursos(View view){
		Intent intent = new Intent(this, Cursos.class);
		startActivity(intent);
		this.finish();
	}
	
	public void facebook(View view){
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/apolosoft?fref=ts"));
		startActivity(intent);
	}
	
	public void twitter(View view){
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/apolosoft"));
		startActivity(intent);
	}
	
	public void volverMenu(View view){
		this.finish();
	}
	
	private boolean hayInternet() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			return true;
		} else {
			return false;
		}
	}
	
	public void opinar(View view){
		if (hayInternet()) {
			this.webService = new WebService();
			String nombre = this.txtNombre.getText().toString();
			String email = this.txtEmail.getText().toString();
			String mensaje = this.txtMensaje.getText().toString();

			if (nombre.equals("") || email.equals("") || mensaje.equals("")) {
				Toast.makeText(this, "Debes completar todos los campos.",
						Toast.LENGTH_LONG).show();
			} else if (!email
					.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) {
				Toast.makeText(this, "Debes ingresar un email valido.",
						Toast.LENGTH_LONG).show();
			} else {
				new Enviar().execute(nombre, email, mensaje);
			}
		} else {
			Toast.makeText(this, "No dispones de una conexión a internet.", Toast.LENGTH_LONG).show();
		}
	}
	
	private class Enviar extends AsyncTask<String, Void, Boolean> {

		private Dialog dialog = new Dialog(Contacto.this);

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.setTitle("Enviando información...");
			dialog.setCancelable(false);
	        dialog.show();
		}

		@Override
		protected Boolean doInBackground(String... arg0) {
			String[] parametros = { "contacto", arg0[0], arg0[1], arg0[2],"Apo-01" };
			JSONArray respuesta = webService.conectar(parametros);
			try {
				JSONObject res = respuesta.getJSONObject(0);
				if (res.getBoolean("respuesta")) {
					return true;
				} else {
					return false;
				}
			} catch (JSONException e) {
				return false;
			}
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			dialog.dismiss();
			if(result){
				Toast.makeText(getApplicationContext(), "Tu mensaje ha sido registrado correctamente.\n" +
						"Gracias por opinar.", Toast.LENGTH_LONG).show();
				txtNombre.setText("");
				txtEmail.setText("");
				txtMensaje.setText("");
			}
			else{
				Toast.makeText(getApplicationContext(), "Lo sentimos.\n" +
						"Tu mensaje no se ha registrado correctamente.\n" +
						"Por favor intentalo de nuevo mas tarde.", Toast.LENGTH_LONG).show();
			}
		}
	}
}
